package com.woniuxy.power.core.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.CommonResult;
import com.woniuxy.power.core.mapper.YdStationMapper;
import com.woniuxy.power.core.model.YdStation;
import com.woniuxy.power.core.service.YdStationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gmx
 * @since 2021-06-29
 */
@Api(tags = "电站相关接口")
@RestController
@RequestMapping("/ydStation")
public class YdStationController {
    @Resource
    private YdStationMapper ydStationMapper;

    @Resource
    private YdStationService ydStationService;

    /**
     * Description : 查询所有电站  <br/>
     * ChangeLog : 1. 创建 (2021/7/2 16:07 [Hello Mr. Guo]);
     *
     * @return com.woniuxy.common.CommonResult
    */
    @ApiOperation("查询所有电站")
    @GetMapping("/selectAll")
    public CommonResult selectAll(){
        List<YdStation> ydStations = this.ydStationService.list();
        return CommonResult.success(ydStations);
    }


    /**
     * Description : 查询单个电站  <br/>
     * ChangeLog : 1. 创建 (2021/7/2 16:07 [Hello Mr. Guo]);
     * @param id
     * @return com.woniuxy.common.CommonResult
    */
    @ApiOperation("查询单个电站")
    @GetMapping("/selectOne/{id}")
    public CommonResult selectOne(@PathVariable("id") Integer id){
        YdStation ydStation = this.ydStationMapper.selectById(id);
        return CommonResult.success(ydStation);
    }

    @ApiOperation("发布充电桩信息")
    @PostMapping("/insert")
    public CommonResult saveStation(@RequestBody YdStation ydStation){
        this.ydStationService.save(ydStation);
        return CommonResult.success();
    }


    @ApiOperation("查询所有电站")
    @GetMapping("selectStation")
    public CommonResult<PageInfo> selectStation(int startpage , int pagesize){
        PageHelper.startPage(startpage, pagesize);
        Page<YdStation> page = (Page<YdStation>) this.ydStationService.selectStation();
        PageInfo<YdStation> pageInfo = new PageInfo<>(page);
        return CommonResult.success(pageInfo);
    }

    @ApiOperation("上架充电桩")
    @PostMapping("updateStatues")
    public CommonResult updateStatus(@RequestBody YdStation ydStation){
        int i = this.ydStationService.updateStatus(ydStation);
        if (i > 0){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }


    @ApiOperation("下架充电桩")
    @PostMapping("updateDisStatues")
    public CommonResult updateDisStatus(@RequestBody YdStation ydStation){
        int i = this.ydStationService.updateDisStatus(ydStation);
        if (i > 0){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }
}

